Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import os | |
| # 1. Groq API key | |
| API_KEY = os.environ.get("GROQ_API_KEY") | |
| def chat_func(msg, history, level): | |
| url = "https://api.groq.com/openai/v1/chat/completions" | |
| headers = { | |
| "Authorization": f"Bearer {API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| # System Prompt mein 'level' ka istemal (Assignment requirement) | |
| messages = [{"role": "system", "content": f"You are a professional English teacher for {level} level students. Adjust your vocabulary and explanations accordingly."}] | |
| for entry in history: | |
| if isinstance(entry, dict): | |
| messages.append({"role": entry["role"], "content": entry["content"]}) | |
| else: | |
| messages.append({"role": "user", "content": entry[0]}) | |
| messages.append({"role": "assistant", "content": entry[1]}) | |
| messages.append({"role": "user", "content": msg}) | |
| payload = { | |
| "model": "llama-3.1-8b-instant", | |
| "messages": messages, | |
| "temperature": 0.7 | |
| } | |
| try: | |
| r = requests.post(url, headers=headers, json=payload, timeout=20) | |
| response_data = r.json() | |
| if "choices" in response_data: | |
| return response_data["choices"][0]["message"]["content"] | |
| return "Error: API Response issues." | |
| except Exception as e: | |
| return f"System Error: {str(e)}" | |
| # UI Layout | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 📘 AI English Teacher") | |
| # Dropdown for Level Selection | |
| level_dropdown = gr.Dropdown( | |
| choices=["Beginner", "Intermediate", "Advanced"], | |
| label="Select Your English Level", | |
| value="Intermediate" | |
| ) | |
| # ChatInterface ko 'level_dropdown' ke saath connect kiya | |
| gr.ChatInterface( | |
| fn=chat_func, | |
| additional_inputs=[level_dropdown] # Ye dropdown ko interface ke 'Additional Inputs' button mein daal dega | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |