import gradio as gr import os import time import google.generativeai as genai import re genai.configure(api_key=os.environ.get('gemini_key')) def to_markdown(text): text = re.sub(r"\*{2}(.*?)\*{2}|_{2}(.*?)_{2}", r"\1", text) text = re.sub(r"\*(.*?)\*|_(.*?)_", r"\1", text) text = re.sub(r"`(.*?)`", r"\1", text) text = re.sub(r'^#{1,6} ', '', text, flags=re.MULTILINE) return text prompt=""" Your description: "You are a math expert." Instraction: " 1. First analize the question carefully. 2. First ask user if him need an explanation if they sey yes in that case create a proper plan to solve it and provide it and provide the formulas you are going to use in this solution then solve the question by using your previously genareted plans and formulas after that recheck the solution if your solution is not correct then correct it, otherwise if the denied to explai then just solve the question and provide the steps only and recheck it don't explain. 3. Never use LaTex format. 4. Never provide output in markdown language. 5. Never provide anything accept math related topic. 6. Your name is mathai and you are powered by gemini-1.5 pro and you are a powerfull math ai. 7. Be respectfull by using 'sir' everytime when you speaking. 8. you can use emojis in normal conversation to make it more exiting. 9. If user ask for any formula then provide only the formula don't explan if user not told to do. 10. If user ask for any defination then provide the defination only." """ model = genai.GenerativeModel(model_name="gemini-1.5-pro-latest",system_instruction=prompt) massege=[ {"role": "user","parts": [""]}, {"role": "model","parts": [""]}, ] convo = model.start_chat(history=massege) def gettext(path): sample_file = genai.upload_file(path=path,display_name="Sample drawing") model = genai.GenerativeModel(model_name="models/gemini-1.5-pro-latest") response = model.generate_content(["Get the text from the picture and convert it into english the provide only the english version of the text.",sample_file]) main(response.text,0) def main(text,his): if not text['files']: massege.append({"role": "user","parts": [f"{text['text']}"]},) convo.send_message(f"{text['text']}") #genai.upload_file(path=text['files'][0],display_name="Sample drawing") output=(convo.last.text) massege.append({"role": "model","parts": [f"{output}"]},) return (f"{to_markdown(output)}") else: sample_file=genai.upload_file(path=text['files'][0],display_name="Sample drawing") #model = genai.GenerativeModel(model_name="models/gemini-1.5-pro-latest") question = model.generate_content([f"Get the text from the picture and convert it into english the provide only the english version of the text.",sample_file]) massege.append({"role": "user","parts": [f"{question}"]},) #response = model.generate_content([f"{text['text']}",sample_file]) convo.send_message(f"provided question: {question}\nUser request: {text['text']}") output=(convo.last.text) #massege.append({"role": "model","parts": [f"{response.text}"]},) massege.append({"role": "model","parts": [f"{output}"]},) return (f"{to_markdown(output)}") #gr.ChatInterface(main).launch() iface=gr.ChatInterface(fn=main,theme=gr.themes.Soft(),title="Math AI", multimodal=True,autofocus=True,fill_height=True) iface.css=""" body{ background-color: #222; color: #eee; } """ iface.launch()