ek0212/crosswordTest
0
1import gradio as gr2from typing import List, Tuple3from together import Together4 5class FlashcardGenerator:6 def __init__(self):7 """Initialize the flashcard generator with Together AI client."""8 self.client = Together()9 10 def generate_flashcards(self, topic: str, num_flashcards: int = 8) -> List[Tuple[str, str]]:11 prompt = f"""Generate {num_flashcards} flashcards on the topic '{topic}'.12 Each flashcard should consist of:13 - A question related to the topic14 - The answer to the question15 Format each flashcard like this: Q: question | A: answer16 17 Now generate {num_flashcards} flashcards for the topic: {topic}"""18 19 response = self.client.chat.completions.create(20 model="mistralai/Mixtral-8x7B-Instruct-v0.1",21 messages=[{"role": "user", "content": prompt}],22 temperature=0.7,23 max_tokens=50024 )25 26 content = response.choices[0].message.content27 result = []28 29 for line in content.strip().split('\n'):30 if '|' in line:31 question_answer = line.strip().split('|')32 if len(question_answer) == 2:33 question = question_answer[0].strip().replace("Q:", "").strip()34 answer = question_answer[1].strip().replace("A:", "").strip()35 result.append((question, answer))36 37 return result[:num_flashcards]38 39def generate_flashcards(topic: str) -> List[Tuple[str, str]]:40 """Generate flashcards based on a given topic."""41 if not topic.strip():42 return [("⚠️", "Please enter a topic.")]43 44 generator = FlashcardGenerator()45 flashcards = generator.generate_flashcards(topic)46 47 return flashcards48 49# Create the Gradio interface50iface = gr.Interface(51 fn=generate_flashcards,52 inputs=gr.Textbox(53 label="Enter a topic for your flashcards",54 placeholder="e.g., space exploration, ancient history, computer programming"55 ),56 outputs=gr.Dataframe(57 headers=["Question", "Answer"], 58 datatype=["str", "str"],59 label="Flashcards"60 ),61 title="📚 AI-Powered Flashcard Generator",62 description="Enter any topic to generate a set of flashcards. Perfect for studying and reviewing any subject!",63 examples=[64 ["space exploration"],65 ["ancient history"],66 ["computer programming"],67 ["cooking and food"],68 ["world geography"]69 ]70)71 72if __name__ == "__main__":73 iface.launch()